<template>
  <el-dialog
    v-model="visible"
    :title="title"
    width="600px"
    append-to-body
    :before-close="close"
  >
    <el-form
      :model="editForm"
      ref="editFormRef"
      label-width="80px"
      :rules="rules"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="名称" prop="name">
            <el-input
              v-model="editForm.name"
              placeholder="请输入名称"
              maxlength="30"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="类型" prop="type">
            <el-select v-model="editForm.type" placeholder="请输入名称" maxlength="30">
              <dictionary-option code="dsType"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="driver" prop="mobilePhone">
            <el-input
              v-model="editForm.driver"
              placeholder="请输入driver"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="连接地址" prop="mobilePhone">
            <el-input
              v-model="editForm.hostUrl"
              placeholder="请输入driver"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="用户名" prop="email">
            <el-input
              v-model="editForm.username"
              placeholder="请输入用户名"
              maxlength="50"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="用户密码" prop="password">
            <el-input
              v-model="editForm.password"
              placeholder="请输入用户密码"
              type="password"
              maxlength="30"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="token" prop="token">
            <el-input
              v-model="editForm.token"
              type="textarea"
              placeholder="请输入内容"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="url" prop="url">
            <el-input
              v-model="editForm.url"
              :rows="6"
              type="textarea"
              placeholder="请输入内容"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="handleSave">确 定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>
import {getCurrentInstance, ref} from 'vue'

import {getById, add, update} from '@/api/sync/ds'

const {proxy} = getCurrentInstance()

const visible = ref(false)
const title = ref('')
const rules = ref({
  name: [{required: true, message: '名称不能为空', trigger: 'blur'}]
})
const editForm = ref({
  id: undefined,
})

/**
 * 显示弹窗
 */
async function show(id, isCopy) {
  visible.value = true
  if (id) {
    if (isCopy) {
      title.value = '复制连接';
      // 获取用户信息
      const {data} = await getById(id)
      editForm.value = data
      editForm.value.id = undefined
    } else {
      title.value = '修改连接';
      // 获取用户信息
      const {data} = await getById(id)
      editForm.value = data
      editForm.value.type = editForm.value.type + ''
    }

  } else {
    title.value = '新增连接'
    editForm.value.id = undefined
  }
}

/**
 * 关闭
 */
function close() {
  visible.value = false
  reset()
}

/**
 * 重置
 */
function reset() {
  proxy.resetForm('editFormRef')
}

/**
 * 提交数据
 */
const emit = defineEmits(null)

function handleSave() {
  proxy.$refs.editFormRef.validate(async (valid) => {
    if (valid) {
      // 修改
      if (editForm.value.id) {
        await update(editForm.value)
        proxy.$modal.msgSuccess('修改用户成功')
      } else {
        // 新增
        await add(editForm.value)
        proxy.$modal.msgSuccess('新增用户成功')
      }
      emit('fetch-data')
      close()
    }
  })
}

defineExpose({
  show
})
</script>
